package com.ruoyi.system.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.BaiLike;
import com.ruoyi.system.service.IBaiLikeService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 点赞Controller
 * 
 * @author saruri
 * @date 2024-09-16
 */
@RestController
@RequestMapping("/like")
public class BaiLikeController extends BaseController
{
    @Autowired
    private IBaiLikeService baiLikeService;

    /**
     * 查询点赞列表
     */

    @GetMapping("/list")
    public TableDataInfo list(BaiLike baiLike)
    {
        startPage();
        List<BaiLike> list = baiLikeService.selectBaiLikeList(baiLike);
        return getDataTable(list);
    }

    /**
     * 导出点赞列表
     */
    @RequiresPermissions("system:like:export")
    @Log(title = "点赞", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BaiLike baiLike)
    {
        List<BaiLike> list = baiLikeService.selectBaiLikeList(baiLike);
        ExcelUtil<BaiLike> util = new ExcelUtil<BaiLike>(BaiLike.class);
        util.exportExcel(response, list, "点赞数据");
    }

    /**
     * 获取点赞详细信息
     */

    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(baiLikeService.selectBaiLikeById(id));
    }


    @PostMapping(value = "/status")
    public AjaxResult getStatus(@RequestBody BaiLike baiLike )
    {
        BaiLike existingLike = baiLikeService.selectBaiLike(baiLike);
        System.out.println("查询点赞状态");
        System.out.println(existingLike);
        return AjaxResult.success(existingLike);
    }

    @PostMapping("/count")
    public AjaxResult getLikeCount(@RequestBody BaiLike baiLike) {
        // 调用 Service 层查询点赞记录总数
        int likeCount = baiLikeService.getLikeCountByConditions(baiLike);
        return AjaxResult.success(likeCount);
    }

    /**
     * 新增点赞
     */
    @RequiresPermissions("system:like:add")
    @Log(title = "点赞", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaiLike baiLike)
    {
        return toAjax(baiLikeService.insertBaiLike(baiLike));
    }

    @PostMapping("/toggleLike")
    public AjaxResult toggleLike(@RequestBody BaiLike baiLike) {
        System.out.println("参数"+baiLike.getUserId()+" 地址"+ baiLike.getUrl());
        // 查询用户是否已经点赞
        BaiLike existingLike = baiLikeService.selectByUserAndUrl(baiLike);
        System.out.println("用户是否点赞"+existingLike);

        if (existingLike != null) {
            // 如果已经有记录，则互换 status
            existingLike.setStatus(existingLike.getStatus() == 1 ? 0 : 1);
            return toAjax(baiLikeService.updateBaiLike(existingLike));
        } else {
            // 如果没有记录，则新增
            baiLike.setStatus(1); // 默认状态为 1 (点赞)
            return toAjax(baiLikeService.insertBaiLike(baiLike));
        }
    }



    /**
     * 修改点赞
     */
    @RequiresPermissions("system:like:edit")
    @Log(title = "点赞", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaiLike baiLike)
    {
        return toAjax(baiLikeService.updateBaiLike(baiLike));
    }

    /**
     * 删除点赞
     */
    @RequiresPermissions("system:like:remove")
    @Log(title = "点赞", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(baiLikeService.deleteBaiLikeByIds(ids));
    }
//TAG_START:BaiLike_Mine_list

// 控制器
@GetMapping("/mine")
public TableDataInfo listMine(BaiLike baiLike) {
    startPage();
    baiLike.setUserId(SecurityUtils.getUserId());
    List<BaiLike> list = baiLikeService.selectBaiLikeListMine(baiLike);
    return getDataTable(list);
}

//TAG_END:BaiLike_Mine_list


}
